<template>
  <div class="page">
    <el-form :model="searchForm" label-position="left" inline label-suffix="：" size="small">
      <div class="flex wap flex-start">
        <el-form-item label="名称">
          <el-input v-model="searchForm.name" clearable></el-input>
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="searchForm.phone" maxlength="11" clearable></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" @click="getData">查询</el-button>
        </el-form-item>
      </div>
    </el-form>
    <div class="flex wap flex-start" style="padding: 0px 0 10px 0">
      <el-button type="success" @click="addData">新增</el-button>
      <div style="width:20px"></div>
      <!-- <el-button type="danger" @click="del">删除</el-button> -->
    </div>
    <el-table :data="tableData" border style="width: 100%"  height="600"  :header-cell-style="{
        background: '#eef1f6',
        color: '#606266',
        'text-align': 'center'
      }"
      :cell-style="{ 'text-align': 'center' }">
      <!-- <el-table-column type="selection"></el-table-column> -->
      <el-table-column prop="empname" label="名称"></el-table-column>
      <el-table-column prop="empphone" label="手机号"></el-table-column>
      <el-table-column prop="empsex" label="性别"></el-table-column>
      <el-table-column prop="graduateschool" label="毕业学校"></el-table-column>
      <el-table-column prop="entrytime" label="加入时间"></el-table-column>
      <el-table-column prop="profession" label="职业"></el-table-column>
      <el-table-column prop="empaddress" label="地址"></el-table-column>
      <el-table-column prop="mainimg" label="展示图片">
        <template slot-scope="scop">
          <el-image fit="fil" style="width: 80px; height: 50px" :src="$Url + '/upload/' + scop.row.empheadimg"
            :preview-src-list="[$Url + '/upload/' + scop.row.empimg1,$Url + '/upload/' + scop.row.empimg2,$Url + '/upload/' + scop.row.empimg3]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="" label="操作" width="120">
        <template slot-scope="scop">
          <el-button type="primary" icon="el-icon-edit" circle @click="edit(scop.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="del(scop.row.empid)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="float:right;padding:10px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog title="教师信息" center :visible.sync="dialogFormVisible" width="60%">
      <el-form :model="addOrUpdateForm" ref="addSetForm" :rules="addrule" label-width="130px" class="edit-form"
        label-suffix="：">
        <div class="flex wape flex-start">
          <el-form-item label="姓名" prop="empname">
            <el-input type="text" placeholder="请输入姓名" v-model="addOrUpdateForm.empname"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="empphone">
            <el-input type="text" placeholder="请输入手机号" maxlength="11" v-model="addOrUpdateForm.empphone"></el-input>
          </el-form-item>
          <el-form-item label="身份证号" prop="empIdCard" v-if="!addOrUpdateForm.empid">
            <el-input type="text" placeholder="请输入身份证号" v-model="addOrUpdateForm.empIdCard"></el-input>
          </el-form-item>
          <el-form-item label="毕业院校" prop="graduateschool">
            <el-input type="text" placeholder="请输入毕业院校" v-model="addOrUpdateForm.graduateschool"></el-input>
          </el-form-item>
          <el-form-item label="专业" prop="profession">
            <el-input type="text" placeholder="请输入专业" v-model="addOrUpdateForm.profession"></el-input>
          </el-form-item>

          <el-form-item label="性别" prop="empsex" v-if="!addOrUpdateForm.empid">
            <el-radio-group v-model="addOrUpdateForm.empsex">
              <el-radio label="男">男</el-radio>
              <el-radio label="女">女</el-radio>
            </el-radio-group>
          </el-form-item>
        </div>

        <el-form-item label="简介" prop="empinfo">
          <el-input type="textarea" :autosize="{ minRows: 2 }" placeholder="请输入教师简介" v-model="addOrUpdateForm.empinfo"></el-input>
        </el-form-item>
        <el-form-item label="住址" prop="empaddress">
          <el-input type="text" placeholder="请输入住址" v-model="addOrUpdateForm.empaddress"></el-input>
        </el-form-item>

        <el-form-item label="离职时间" prop="leavetime" v-if="addOrUpdateForm.empid">
          <el-date-picker v-model="addOrUpdateForm.leavetime" type="date" placeholder="选择离职日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入职时间" prop="entrytime" v-else>
          <el-date-picker v-model="addOrUpdateForm.entrytime" type="date" placeholder="选择入职日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <div class="pic-box flex wape flex-between">
          <el-form-item label="主页图" prop="empheadimg">
            <upload v-model="addOrUpdateForm.empheadimg" />
          </el-form-item>
          <el-form-item label="展示图1" prop="empimg1">
            <upload v-model="addOrUpdateForm.empimg1" />
          </el-form-item>
          <el-form-item label="展示图2" prop="empimg2">
            <upload v-model="addOrUpdateForm.empimg2" />
          </el-form-item>
          <el-form-item label="展示图3" prop="empimg3">
            <upload v-model="addOrUpdateForm.empimg3" />
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSave" :loading="saveLoading">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { findEmp, updateEmp, addEmp, delEmp } from "@/api/user";
import upload from "@/components/uploadFile";
export default {
  components: {
    upload
  },
  data() {
    return {
      total: 0,
      pageNum: 1,
      pageSize: 10,
      tableData: [],
      dialogFormVisible: false,
      saveLoading: false,
      searchForm: {
        name: "",
        phone: ""
      },
      addOrUpdateForm: {
        empid: '',
        leavetime: '',
        entrytime: '',
        profession: '',
        graduateschool: '',
        empaddress: '',
        empphone: '',
        empIdCard: '',
        empsex: '',
        empname: '',
        empinfo: '',
        empheadimg: '',
        empimg1: '',
        empimg2: '',
        empimg3: ''
      },
      addrule: {
        empname: [
          { required: true, message: '必须填写姓名', trigger: 'change' }
        ],
        empinfo: [
          { required: true, message: '必须填写教师简介', trigger: 'change' }
        ],
        profession: [
          { required: true, message: '必须填写专业', trigger: 'change' }
        ],
        empsex: [
          { required: true, message: '必须选择性别', trigger: 'change' }
        ],
        graduateschool: [
          { required: true, message: '必须填写毕业院校', trigger: 'change' }
        ],
        empaddress: [
          { required: true, message: '必须填写地址', trigger: 'change' }
        ],
        empphone: [
          { required: true, message: '必须填写家长手机号', trigger: 'change' },
          { pattern: this.$reg.reg_phone, message: '手机号格式错误', trigger: 'change' },
        ],
        empIdCard: [
          { required: true, message: '必须填写身份证号', trigger: 'change' },
          { pattern: this.$reg.reg_idcard, message: '身份证号格式错误', trigger: 'change' },
        ],
        entrytime: [
          { required: true, message: '必须填写入职时间', trigger: 'change' }
        ]

      }
    }
  },
  computed: {
    queryData() {
      return {
        empname: this.searchForm.name,
        empphone: this.searchForm.phone,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    async getData() {
      const res = await findEmp.post(this.queryData);
      console.log(res, 555);

      if (res.resultCode === "000000") {
        this.tableData = res.data.employeeList;
        this.total = res.data.count;
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    addData() {
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['addSetForm'].resetFields();
      })
      this.addOrUpdateForm = {
        empid: '',
        entrytime: '',
        profession: '',
        graduateschool: '',
        empaddress: '',
        empphone: '',
        empIdCard: '',
        empsex: '',
        empname: '',
        empinfo: '',
        empheadimg: '',
        empimg1: '',
        empimg2: '',
        empimg3: ''
      }
    },
    edit(row) {
      this.dialogFormVisible = true
      const { empid, empname, empphone, empaddress, graduateschool, profession, leavetime, empinfo, empheadimg, empimg1, empimg2, empimg3 } = row
      this.addOrUpdateForm = {
        empid,
        leavetime,
        profession,
        graduateschool,
        empaddress,
        empphone,
        empname,
        empinfo,
        empheadimg,
        empimg1,
        empimg2,
        empimg3
      }
    },
    addSave() {
      this.$refs.addSetForm.validate(async (valid) => {
        if (valid) {
          this.saveLoading = true
          const res = this.addOrUpdateForm.empid ? await updateEmp.post(this.addOrUpdateForm) : await addEmp.post(this.addOrUpdateForm);
          this.saveLoading = false
          if (res.resultCode === "000000") {
            this.$message.success(res.resultInfo);
            this.dialogFormVisible = false
            this.getData()
          } else {
            this.$message.error(res.resultInfo);
          }
        } else {
          return false;
        }
      });
    },
    del(id) {
      this.$confirm("此操作将删除该项目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.delItem(id)
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async delItem(id) {
      const res = await delEmp.post({ empid: id });
      if (res.resultCode === "000000") {
        this.$message.success(res.resultInfo);
        this.getData()
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getData();
    }
  }
};
</script>
<style lang="less" scoped></style>
